package com.illtamer.infinite.sillage.controller.admin;

import com.illtamer.infinite.sillage.annotation.Convert;
import com.illtamer.infinite.sillage.annotation.SecurityUser;
import com.illtamer.infinite.sillage.entity.AjaxResult;
import com.illtamer.infinite.sillage.mapper.BlogTagMapper;
import com.illtamer.infinite.sillage.pojo.Blog;
import com.illtamer.infinite.sillage.pojo.User;
import com.illtamer.infinite.sillage.service.BlogService;
import com.illtamer.infinite.sillage.service.CommentService;
import com.illtamer.infinite.sillage.service.TagService;
import com.illtamer.infinite.sillage.service.TypeService;
import com.illtamer.infinite.sillage.vo.BlogQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/blog")
public class BlogController {

    @Resource(name = "blogService")
    private BlogService blogService;

    @Resource(name = "typeService")
    private TypeService typeService;

    @Resource(name = "tagService")
    private TagService tagService;

    @RequestMapping
    public String blogs(@SecurityUser User user,
                        @RequestParam(name = "index", required = false, defaultValue = "1") Integer index, Model model) {
        model.addAttribute("avatar", user.getAvatar());
        // specify
        model.addAttribute("pages", blogService.listBlogs(index, 10));
        model.addAttribute("types", typeService.listThinTypes());
        return "admin/blog";
    }

    @RequestMapping("/publish")
    public String blogPublish(@SecurityUser User user,
                              @RequestParam(name = "id", required = false) Integer id, Model model) {
        model.addAttribute("avatar", user.getAvatar());
        model.addAttribute("types", typeService.listThinTypes());
        model.addAttribute("tags", tagService.listThinTags());
        if (id != null)
            model.addAttribute("blog", blogService.selectById(id));
        return "admin/blog-publish";
    }

    @PostMapping("/search")
    public String search(BlogQuery blogQuery,
                         @RequestParam("page") Integer page, Model model) {
        model.addAttribute("pages", blogService.listBlogs(blogQuery, page, 10));
        return "admin/blog::blogList";
    }

    @ResponseBody
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Integer id) {
        // TODO 删除对应 sillage_blog_tag
        Boolean deleted = blogService.deleteById(id);
        return deleted ? AjaxResult.success() : AjaxResult.warn("数据源不存在");
    }

    @ResponseBody
    @PostMapping("/publish/save")
    public AjaxResult insert(@SecurityUser User user,
                             @Convert Blog blog) {
        blog.setUser(user);
        if (blog.getId() != null) {
            return blogService.updateBlog(blog) ? AjaxResult.success("修改成功") : AjaxResult.error();
        }
        Boolean saved = blogService.insertBlog(blog);
        return saved ? AjaxResult.success() : AjaxResult.error();
    }

}
